﻿using ProjectCore.Domain.Model.Entity;
using ProjectCore.Domain.Repository.Interfaces;
using ProjectCore.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using ProjectCore.Domain.DomainService;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
using ProjectCore.Common;
using System.Linq.Expressions;

namespace ProjectCore.Infrastructure.DomainService
{
    public class EvalDomainService : IEvalDomainService
    {
        private readonly IBaseRepository<Eval> baseRepository;
        private readonly MyContext myContext;
        public EvalDomainService(IBaseRepository<Eval> _baseRepository, MyContext _myContext)
        {
            baseRepository = _baseRepository;
            myContext = _myContext;
        }

        //反填
        public async Task<Eval> GetEval(int Id)
        {
            try
            {
                return await myContext.Evals.FindAsync(Id);
            }
            catch (Exception) { throw; }
        }

        //获取评论集合
        public async Task<HeaderResult<List<Eval>>> GetEvals(int pageIndex, int pageSize)
        {
            try
            {
                Expression<Func<Eval, bool>> where = e => true;
                var adminList = await baseRepository.LoadEntityListAsync(where, e => e.Sort, "asc", pageIndex, pageSize);
                int count = await baseRepository.GetEntitiesCountAsync(where);
                HeaderResult<List<Eval>> result = new HeaderResult<List<Eval>>()
                {
                    IsSucceed = true,
                    Result = adminList,
                    Total = count,
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
        //逻辑删除
        public async Task<int> IsDel(int Id)
        {
            try
            {
                Eval eval = myContext.Evals.Where(x => x.EvalID.Equals(Id)).FirstOrDefault();
                eval.IsDeleted = true;
                return await myContext.SaveChangesAsync();
                //string sql = $"update Evals set IsDeleted = 1 where EvalID = {Id}";
                //return myContext.Database.ExecuteSqlCommand(sql);
            }
            catch (Exception)
            {

                throw;
            }

        }

        //修改
        public async Task<int> UptEval(Eval eval)
        {
            try
            {
                return await baseRepository.UpdateEntityAsync(eval);
            }
            catch (Exception) { throw; }
        }
    }
}
